<?php
define( '__ESPACE__', 'admin');
$prefix = '../../';
require_once($prefix.'starter.php');

ob_start();

$PWE_admin = new controler_admin();

/* parsing get datas */
$maxWidth = (isset($_GET['max_width']) && $_GET['max_width']>0)?$_GET['max_width']:0;
$maxHeight = (isset($_GET['max_height']) && $_GET['max_height']>0)?$_GET['max_height']:0;

$modeleId = (isset($_GET) && isset($_GET['modele_id']) && $_GET['modele_id']>0)?$_GET['modele_id']:0;
$modeleType = (isset($_GET) && isset($_GET['modele']))?$_GET['modele']:'';
$module = (isset($_GET) && isset($_GET['module']))?$_GET['module']:'';
$modele = $PWE_admin->getModele($modeleType, $modeleId, $module);
$media = null;

$listeActiveMediaId = array();

if($modele!==null){
    if($modeleType==="galerie"){
        $listeActiveMedia = $modele->getMedias();

        foreach($listeActiveMedia as $activeMedia){
            $listeActiveMediaId[] = $activeMedia->getId();
        }
    }elseif($modeleType==='logo'){
        $listeActiveMediaId[] = $modele->getId();
    }else{
        $listeActiveMediaId[] = $modele->getMediaId();
    }
    
    if(isset($_GET) && isset($_GET['media_id']) && $_GET['media_id']>0){
        $selectedMedia = new modele_media($_GET['media_id']);
        if($modele!==null){
            $modele->addMedia($_GET['media_id']);
        }
    }
}

$liste = modele_media::getListe();
?>
<!DOCTYPE html>
<html>
    <head>
        <title>Selectioinner un média</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta name="robots" content="noindex,nofollow">
        <link href='http://fonts.googleapis.com/css?family=Grand+Hotel' rel='stylesheet' type='text/css'>
        <link href="../css/style.css" type="text/css" rel="stylesheet" media="all"/>
        <script src="../../js/libs/jquery/jquery.js" type="text/javascript"></script>
        <link href="<?php echo $PWE_admin->getUiThemePath(); ?>" type="text/css" rel="stylesheet" media="all"/>
        <script src="../../js/libs/jqueryui/jquery-ui.js" type="text/javascript"></script>
        <script>
            var origine = window.top;
            <?php if(isset($selectedMedia)&&$selectedMedia->getId()>0):?>
                origine.addMedia({modele: '<?php echo $modeleType; ?>',module: '<?php echo $module; ?>',order: 0, media_id: <?php echo $selectedMedia->getId(); ?>, modele_id: <?php echo $_GET['modele_id']; ?>, path: '<?php echo PWE::getBaseURL().$selectedMedia->getPath(); ?>', title: "<?php echo $selectedMedia->getTitle(); ?>", legende: "<?php echo $selectedMedia->getLegende(); ?>",maxWidth: <?php echo $maxWidth; ?>,maxHeight: <?php echo $maxHeight; ?>});
            <?php endif; ?>
            
            $(document).ready(function(){
                /* bouton forumaires */
                $('.pwe-ui-btn').button();
            });
        </script>
    </head>
    <body>
        <table id="liste_media" class="liste ui-widget" width="100%">
            <thead class="ui-widget-header">
                <tr>
                    <th>Titre</th>
                    <th>fichier</th>
                    <th>Légende</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach($liste as $media): ?>
                <tr id="media_<?php echo $media->getId(); ?>" class="ui-widget-content">
                    <td>
                        <span><?php echo $media->getTitle(); ?></span>
                        <?php if(!in_array($media->getId(),$listeActiveMediaId)): ?>
                        <br/>
                        <a href="?modele=<?php echo $modeleType; ?>&module=<?php echo $module; ?>&modele_id=<?php echo $_GET['modele_id']; ?>&media_id=<?php echo $media->getId(); ?>&maxWidth=<?php echo $maxWidth; ?>&maxHeight=<?php echo $maxHeight; ?>">Ajouter</a>
                        <?php endif; ?>
                    </td>
                    <?php $thumb = ($media->getMediaType()=='image')?$media->getUrl($prefix):PWE::getBaseURL().'admin/css/gfx/logoPdf.png'; ?>
                    <td><?php echo '<img src="'.$thumb.'" width="90px" title="'.$media->getUrl($prefix).'" />'; ?></td>
                    <td><?php echo $media->getLegende(); ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
            <tfoot></tfoot>
        </table>
        <div align="right"><br/>
            <a class="pwe-ui-btn" href="javascript:origine.openAddMedia(<?php echo $_GET['modele_id']; ?>,'<?php echo $modeleType; ?>',<?php echo $maxWidth; ?>,<?php echo $maxHeight; ?>,'<?php echo $module; ?>');">Nouveau média</a>
        </div>
    </body>
</html>